package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragment;
import com.zzptc.zzsgz.bdsjws.services.MyDownLoadService;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/16.
 */

public class UpdateVersionFragment extends BaseFragment {
    @BindView(R.id.tv_fregment)
    TextView tvFregment;
    Unbinder unbinder;
    @BindView(R.id.butt_yes1)
    Button buttYes1;
    @BindView(R.id.butt_no1)
    Button buttNo1;
    Unbinder unbinder1;
    VersionInfo versionInfo;

    @Override
    public void intDateView(View view) {
        //更新TextView  getArgument
        Bundle bundle = getArguments();
        versionInfo = (VersionInfo) bundle.getSerializable("versioninfo");
        //tvFregment.setText(versionInfo.getInfo());
        if (versionInfo != null) {
            tvFregment.setText(versionInfo.getInfo());
            buttNo1.setVisibility(View.VISIBLE);//让取消按钮显示出来
        } else {
            tvFregment.setText(getMsg());
        }
        //tvFregment.setText(versionInfo.getInfo());

    }

    @Override
    public int getContentXml() {
        return R.layout.versionfragment;//缺少布局文件
    }

    //写一个构造方法 构造UpdateVersionFragment对象  方法的重载
    public static UpdateVersionFragment getUpdateVersionFragment(String msg) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        updateVersionFragment.setArguments(bundle);//设置值 传递给Activity
        return updateVersionFragment;
    }

    //写一个构造方法 构造UpdateVersionFragment对象
    public static UpdateVersionFragment getUpdateVersionFragment(VersionInfo versionInfo) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("versioninfo", versionInfo);
        updateVersionFragment.setArguments(bundle);//设置值 传递给Activity  载体是Bundle
        return updateVersionFragment;
    }

    public String getMsg() {
        Bundle bundle = getArguments();
        return bundle.getString("msg");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(unbinder!=null){
            unbinder.unbind();
        }
    }

    @OnClick({R.id.butt_yes1, R.id.butt_no1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.butt_yes1:   //1创建通知栏 显示下载进度条 2下载apk 3安装
              if(versionInfo!=null){
                  Intent intent=new Intent();
                  intent.setClass(getActivity(), MyDownLoadService.class);
                  //传字符串过去
                  intent.putExtra("aplurl",versionInfo.getApkurl());
                 MyApplication.getContext().startService(intent);
              }
                dismiss();
                break;
            case R.id.butt_no1:
                dismiss();
                break;
        }
    }
}
